package com.zhkj.txg.module.mine.ui

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import com.tbruyelle.rxpermissions2.RxPermissions
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.vm.FeedbackViewModel
import com.zhkj.txg.utils.ConfigManager
import kotlinx.android.synthetic.main.activity_feedback.*


class FeedbackActivity : ViewModelActivity<FeedbackViewModel>() {
    override fun setViewModel(): FeedbackViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleFeedback
    override val layoutResId: Int = R.layout.activity_feedback


    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, FeedbackActivity::class.java)
            activity.startActivity(intent)
        }
    }


    override fun initUI() {

        titleFeedback.setRightClickListener(View.OnClickListener {
            val rxPermission = RxPermissions(this)
            rxPermission.request(Manifest.permission.CALL_PHONE)
                .subscribe {
                    if (it) {
                        val builder = AlertDialog.Builder(this)
                            .setTitle("联系客服")
                            .setMessage(ConfigManager.getCustomerMobile())
                            .setPositiveButton("确定") { dialog, which ->
                                dialog.cancel()
                                val intent = Intent(Intent.ACTION_CALL);
                                val data = Uri.parse("tel:${ConfigManager.getCustomerMobile()}");
                                intent.data = data
                                startActivity(intent)
                            }
                            .setNegativeButton("取消") { dialog, which ->
                                dialog.cancel()
                            }
                        builder.show()
                    } else {
                        toast("请到设置中开启权限")
                    }
                }
        })

        tvFeedBackSubmit.setOnClickListener {
            val content = etFeedBackContent.text

            if (content.isNullOrEmpty()) {
                toast("请输入问题反馈内容")
                return@setOnClickListener
            }

            viewModel.setFeedback(content.toString(), null)
        }


        val textWatcher: TextWatcher = object : TextWatcher {
            // 输入文本之前的状态
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

            // 输入文本中的状态
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            // 输入文本之后的状态
            override fun afterTextChanged(s: Editable) {
                tvFeedBackSubmit.isEnabled = s.isNotEmpty()
            }
        }

        etFeedBackContent.addTextChangedListener(textWatcher);

    }

    override fun initViewModel() {

        viewModel.feedbackLiveData.observe(this, Observer { response ->
            if (response.status == HttpResponseStatus.Status.SUCCESS) {
                response?.response?.let {
                    val msg = it.msg ?: ""
                    toast(it.msg ?: "")
                    if (it.code == 1) {
                        val builder = AlertDialog.Builder(this)
                            .setMessage(msg)
                            .setPositiveButton("确定") { dialog, which ->
                                dialog.cancel()
                                etFeedBackContent.text = Editable.Factory.getInstance().newEditable("")
                            }
                        builder.show()
                    }
                }
            } else {
                toast(response.exception.toString())
            }
        })
    }
}